#include "irm.h"
#include "sys.h"

static u8 __xdata isIrmReceived;
void irm_init(void)
{
    TIM_InitTypeDef __xdata TIM_InitStructure;
    TIM_InitStructure.TIM_ClkOut = DISABLE;
    TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T;
    TIM_InitStructure.TIM_Interrupt = DISABLE;
    TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload;
    TIM_InitStructure.TIM_Priority = Priority_0;
    TIM_InitStructure.TIM_Run = ENABLE;
    TIM_InitStructure.TIM_Value = 65535 - MAIN_Fosc / 12 / 1000 / 1000 * 13;
    Timer_Inilize(Timer4, &TIM_InitStructure);

    GPIO_InitTypeDef __xdata GPIO_InitStructure;//结构定义
    GPIO_InitStructure.Pin = GPIO_Pin_7;        //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_OUT_PP;      //指定IO的输入或输出方式
    GPIO_Inilize(GPIO_P0, &GPIO_InitStructure); //初始化

    // GPIO_InitStructure.Pin = GPIO_Pin_2;        //指定要初始化的IO,
    // GPIO_InitStructure.Mode = GPIO_OUT_PP;      //指定IO的输入或输出方式
    // GPIO_Inilize(GPIO_P0, &GPIO_InitStructure); //初始化    

    // EXTI_InitTypeDef __xdata EXTI_InitStructure;
    // EXTI_InitStructure.EXTI_Interrupt = ENABLE;
    // EXTI_InitStructure.EXTI_Mode = EXT_MODE_Fall;
    // EXTI_InitStructure.EXTI_Priority = Priority_0;
    // Ext_Inilize(EXT_INT2, &EXTI_InitStructure);
}

void irm_sender(void)
{
    static u8 __xdata toogle;
    if(toogle == 1)
    {
        Timer4_CLKO_Enable();
        Timer4_Run();
        toogle = 0;
    }
    else
    {
        toogle = 1;
        Timer4_Stop();
        Timer4_CLKO_Disable();
        P07 = 0;
    }
}

void irm_receiver(void)
{
    if(isIrmReceived)
    {
        P24 = 1;
        PrintString1("IRM\r\n");
    }
}

// void Ext_INT2 (void) __interrupt INT2_VECTOR
// {
//     if(isIrmReceived == 0)
//         isIrmReceived = 1;
// 	// P24 = !P24;
// 	// WakeUpSource = 3;
// }